package com.example.sample;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.facebook.android.Facebook;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class Image extends Activity {

	private WebView webView;
	private ProgressBar progress;
	private Facebook mFacebook = new Facebook(C.FACEBOOK_APP_ID);
	private Pref pref = new Pref();
	private String mFacebookAccessToken;
//	private Button upButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image);
		mFacebookAccessToken = pref.getAppPreferences(this, "ACCESS_TOKEN");
		mFacebook.setAccessToken(mFacebookAccessToken);

//		ImageView imgView = (ImageView)findViewById(R.id.imageView1);
//		Drawable drawable = LoadImage("http://static.naver.net/www/u/2010/0611/nmms_215646753.gif");
//		imgView.setImageDrawable(drawable);
//		upButton = (Button) this.findViewById(R.id.button1);
		progress = (ProgressBar) this.findViewById(R.id.progressBar1);
		webView = (WebView) this.findViewById(R.id.webView1);
//		webView.setFocusable(false);
//		webView.setBackgroundColor(0);
//		webView.setHorizontalScrollBarEnabled(false);
//		webView.setVerticalScrollBarEnabled(false);
//		webView.setLongClickable(false);
//		webView.setFocusableInTouchMode(false);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setSupportZoom(true);
		webView.setWebChromeClient(new WebChromeClient(){

			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				super.onProgressChanged(view, newProgress);
				
				if(newProgress < 100){
					progress.setVisibility(View.VISIBLE);
				}else{
					progress.setVisibility(View.GONE);
				}
			}
			
		});
		webView.setWebViewClient(new MyWebViewClient());
		
		new GetTask().execute();
		
	//	webView.loadUrl("http://m.facebook.com/album.php?fbid=122566041253937&id=100005016248236&aid=22850");
		
	//	webView.loadDataWithBaseURL(null, getImageHtmlCode("http://static.naver.net/www/u/2010/0611/nmms_215646753.gif"), "text/html", "utf-8", null);
		
	}
	
	private class GetTask extends AsyncTask<String, String, String>{ 
		 
		GetUrl get = new GetUrl();
		@Override
		protected String doInBackground(String... params) {
			return get.getJsonText();
		}

		@Override
		protected void onPostExecute(String result) {
					
			webView.loadUrl(result);

			super.onPostExecute(result);
		}		 
	 }
	
	
	private class MyWebViewClient extends WebViewClient{
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return false;
		}
	}
	
	/*
	public static String getImageHtmlCode(String _imageURL){
		StringBuffer sb = new StringBuffer("<HTML>");
		sb.append("<HEAD>");
		sb.append("</HEAD>");
		sb.append("<BODY>");
		sb.append("<img width=\"100%\" height=\"94%\" src=\""+_imageURL +"\"/>");
		sb.append("</BODY>");
		sb.append("</HTML>");
		return sb.toString();
	}
*/
	
	 
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.image, menu);
		return true;
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
	    	webView.goBack();
	        return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}

}
